package com.ymatou.iapi.trading.operate.testcase;

import java.math.BigDecimal;
import java.util.HashMap;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.OnOrderPaidBean;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.operate.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.trading.operate.service.OnOrderPaidCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;

/**
 * 支付完成通知消息接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_OnOrderPaid {
	private static OnOrderPaidBean onorderpaidBean;
	private static OnOrderPaidCall onorderpaidCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("支付完成通知消息接口");
	}

	@Before
	public void caseUp() {
		onorderpaidBean = new OnOrderPaidBean();
		onorderpaidCall = new OnOrderPaidCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(onorderpaidCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 一个订单下面多个商品, 老买手，新用户
	 * @throws Exception 
	 * 
	 */
	private PlaceOrderResp placeOrder6(int userId) throws Exception {

		if (userId == 0) {

			userId = new OrderCallService().createNewUser();

		}

		PlaceOrderReq placeorderBean = PlaceOrderService
				.getDefaultOrderReq(userId);

		return PlaceOrderService.callPlaceOrderService(placeorderBean);

	}
	// 数据库检查点
	private void verifyDb() {
		DefaultWapper database = new DefaultWapper();
		HashMap<String, Integer> m = new HashMap<String, Integer>();
		m.put("OrderId", onorderpaidBean.getOrderId());
		m.put("TradingStatus", 2);
		m.put("TraddingId", onorderpaidBean.getOrderId());
		// m.put("UserId", acceptorderbean.getSellerId());
		Logger.verifyEquals(2,
				(Integer) database.selectOrder(m).get(0).get("iTradingStatus"),
				"验证订单表状态");
		Logger.verifyEquals(2,
				database.selectTradingStatus(m).get(0).get("iTradingStatus"),
				"验证交易表状态");
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_OnOrderPaid_001() {
		Logger.start(true, "支付完成通知成功");
		try {
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder4();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			Logger.comment("前置数据准备结束");

			onorderpaidBean.setOrderId(orderId);
			onorderpaidCall.setData(onorderpaidBean);
			onorderpaidCall.callService();
			Logger.verifyEquals("ok", onorderpaidCall.getReturnData(),
					"验证接口返回报文");
			verifyDb();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_OnOrderPaid_002() {
		Logger.start(true, "支付完成通知成功");
		try {

			int orderId = 2138529158;

			onorderpaidBean.setOrderId(orderId);
			onorderpaidCall.setData(onorderpaidBean);
			onorderpaidCall.callService();
			Logger.verifyEquals("ok", onorderpaidCall.getReturnData(),
					"验证接口返回报文");

			//verifyDb();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	//@Ignore
	public void Tc_OnOrderPaid_003() {
		Logger.start(true, "支付完成通知成功");
		try {

			PlaceOrderResp placeOrderResp = placeOrder6(30245437);

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
